package com.l4.app.ui;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Thusira
 */
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JFrame implements ActionListener, Runnable {

    public static boolean isDelayApplied = false;
    public static int addedTime;
    private static JButton btn1 = null;
    private static JButton btn2 = null;
    private static JButton btn3 = null;
    private static TrafficSignal green = new TrafficSignal(Color.green);
    private static TrafficSignal yellow = new TrafficSignal(Color.yellow);
    private static TrafficSignal red = new TrafficSignal(Color.red);
    private static TrafficSignal greenP = new TrafficSignal(Color.green);
    private static TrafficSignal redP = new TrafficSignal(Color.red);
    JTextArea area = LogPanel.logArea;

    public TrafficLight() {
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        btn1 = new JButton("Green");
        btn2 = new JButton("Yellow");
        btn3 = new JButton("Red");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);

        green.turnOn(false);
        yellow.turnOn(false);
        red.turnOn(false);
        greenP.turnOn(false);
        redP.turnOn(false);

        JPanel lights = new JPanel(new FlowLayout());
        lights.add(green);
        lights.add(yellow);
        lights.add(red);
        JPanel btnPane = new JPanel(new FlowLayout());
        btnPane.setBounds(0, 100, 100, 100);
        btnPane.add(greenP);
        btnPane.add(redP);

        getContentPane().add(lights);
        getContentPane().add(btnPane);
        pack();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn1) {
            green.turnOn(true);
            yellow.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn2) {
            yellow.turnOn(true);
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn3) {
            red.turnOn(true);
            yellow.turnOn(false);
            green.turnOn(false);
        }
    }

    public void run() {
        try {
            run2();
        } catch (InterruptedException ex) {
            Logger.getLogger(TrafficLight.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void run2() throws InterruptedException {
        Thread currentThread = Thread.currentThread();
        while (true) {
            if (isDelayApplied && addedTime!=0) {
                delayedRoutine(addedTime);
            } else {
                nonDelayedRoutine();
            }
        }

    }

    void delayedRoutine(int delayTime) throws InterruptedException {
        //allow vehicles
        LogPanel.logArea.append("- Pedestrian clearence time added:- "+delayTime+" sec.\n\n");
        System.out.println("---------------DELAY APPLIED-------------");
        green.turnOn(false);
        red.turnOn(true);
        yellow.turnOn(false);
        greenP.turnOn(true);
        redP.turnOn(false);

        Thread.sleep(3000);
        yellow.turnOn(true);
        Thread.sleep(1000);

        //allow pedestrians
        yellow.turnOn(false);
        green.turnOn(true);
        red.turnOn(false);
        greenP.turnOn(false);
        redP.turnOn(true);

        Thread.sleep(7000);
        yellow.turnOn(true);
        Thread.sleep(1000);
        isDelayApplied=false;
    }

    void nonDelayedRoutine() throws InterruptedException {
        //allow vehicles
        System.out.println("---------------DELAY NOT APPLIED-------------");
        green.turnOn(false);
        red.turnOn(true);
        yellow.turnOn(false);
        greenP.turnOn(true);
        redP.turnOn(false);

        Thread.sleep(4000);
        yellow.turnOn(true);
        Thread.sleep(1000);

        //allow pedestrians
        yellow.turnOn(false);
        green.turnOn(true);
        red.turnOn(false);
        greenP.turnOn(false);
        redP.turnOn(true);

        Thread.sleep(3000);
        yellow.turnOn(true);
        Thread.sleep(1000);
    }
}

class TrafficSignal extends JPanel {

    Color on;
    int radius = 40;
    int border = 10;
    boolean active;

    TrafficSignal(Color color) {
        on = color;
        active = true;
    }

    public void turnOn(boolean a) {
        active = a;
        repaint();
    }

    public Dimension getPreferredSize() {
        int size = (radius + border) * 2;
        return new Dimension(size, size);
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());

        if (active) {
            g.setColor(on);
        } else {
            g.setColor(on.darker().darker().darker());
        }
        g.fillOval(border, border, 2 * radius, 2 * radius);
    }
}
